﻿package mortal.game.view.common.tooltip.tooltips
{
    import Message.DB.Tables.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.core.mesh.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.player.weapon.*;
    import mortal.game.view.common.*;

    public class ToolTipFuncPre_3D extends BaseToolTip implements IWindow3D
    {
        protected var _3dBg:GImageBitmap;
        protected var _img2d:Img2D;
        protected var _rect3D:Rect3DObject;
        protected var _bodyPlayer:Md5Mesh;
        protected var _3dRectWidth:int = 340;
        protected var _3dRectHeight:int = 389;
        private var _bmpTitle:GImageBitmap;
        private var _bmpLabel:GImageBitmap;
        protected var _img2dTitle:Img2D;
        protected var _img2dLabel:Img2D;
        private var _config:TSystemTrailer;
        private var _tween:TweenMax;
        private var turnNum:Number = 0;
        private var _dy:int = 0;
        private var _dx:int = 0;
        private var _allScale:Number = 1.5;
        private var _isDisposed:Boolean = true;
        protected var _contentTopOf3DSprite:Sprite;

        public function ToolTipFuncPre_3D()
        {
            this._contentTopOf3DSprite = new Sprite();
            this.createChildren();
            return;
        }// end function

        override public function set data(param1) : void
        {
            this._config = param1 as TSystemTrailer;
            DisplayUtil.removeMe(this._bmpLabel);
            DisplayUtil.removeMe(this._bmpTitle);
            this._bmpTitle.onLoadCompl = this.titleLoadedHandler;
            this._bmpTitle.imgUrl = "funcPreTitle" + this._config.system;
            this._bmpLabel.onLoadCompl = this.labelLoadedHandler;
            this._bmpLabel.imgUrl = "funcPreLabel" + this._config.system;
            this._3dBg.onLoadCompl = this.bgLoadedHandler;
            this._3dBg.imgUrl = ImagesConst.funcPrePic_3dBg;
            return;
        }// end function

        protected function createChildren() : void
        {
            this._3dBg = UIFactory.gImageBitmap(null, 0, 0);
            this._bmpTitle = UIFactory.gImageBitmap(null, 0, 16);
            this._bmpLabel = UIFactory.gImageBitmap(null, 0, 308);
            this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.removeFromeStageHandler);
            return;
        }// end function

        private function registerRect3D() : void
        {
            var _loc_1:* = new Rectangle(0, 0, this._3dRectWidth, this._3dRectHeight);
            this._rect3D = Rect3DManager.instance.registerWindow(true, _loc_1, this, true);
            Rect3DManager.instance.windowShowHander(null, this);
            return;
        }// end function

        private function titleLoadedHandler() : void
        {
            if (this._isDisposed)
            {
                return;
            }
            if (this._img2dTitle)
            {
                this._img2dTitle.dispose();
            }
            this._img2dTitle = new Img2D(null, this._bmpTitle.bitmapData, new Rectangle(0, 0, this._bmpTitle.bitmapData.width, this._bmpTitle.bitmapData.height), false);
            this._img2dTitle.autoDispose = true;
            this._rect3D.addImg(this._img2dTitle);
            this._img2dTitle.x = 174 - this._bmpTitle.width / 2;
            this._img2dTitle.y = 12;
            return;
        }// end function

        private function labelLoadedHandler() : void
        {
            if (this._isDisposed)
            {
                return;
            }
            if (this._img2dLabel)
            {
                this._img2dLabel.dispose();
            }
            this._img2dLabel = new Img2D(null, this._bmpLabel.bitmapData, new Rectangle(0, 0, this._bmpLabel.bitmapData.width, this._bmpLabel.bitmapData.height), false);
            this._img2dLabel.autoDispose = true;
            this._rect3D.addImg(this._img2dLabel);
            this._img2dLabel.x = 170 - this._bmpLabel.width / 2;
            this._img2dLabel.y = 297;
            return;
        }// end function

        private function bgLoadedHandler() : void
        {
            if (this._isDisposed)
            {
                return;
            }
            if (this._img2d)
            {
                this._img2d.dispose();
            }
            this._img2d = new Img2D(null, this._3dBg.bitmapData, new Rectangle(0, 0, this._3dRectWidth, this._3dRectHeight), false);
            this._img2d.autoDispose = true;
            this._rect3D.addImg(this._img2d, Layer3DManager.nearEffect5);
            this._dy = 0;
            this._dx = 0;
            this._allScale = 1.5;
            if (this._bodyPlayer)
            {
                this._bodyPlayer.dispose();
            }
            if (this._config.bone != null)
            {
                this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
                if (this._bodyPlayer.meshUrl != this._config.mesh || this._bodyPlayer.animUrl != this._config.bone)
                {
                    (this._bodyPlayer as ActionPlayer).load(this._config.mesh + ".md5mesh", this._config.bone + ".skeleton", this._config.texture, this._rect3D.renderList);
                }
                this._bodyPlayer.alpha = 1;
                (this._bodyPlayer as ActionPlayer).play();
            }
            else
            {
                var modelTurn:* = function () : void
            {
                var _loc_1:* = turnNum + 0.1;
                turnNum = turnNum + 0.1;
                _bodyPlayer.y = _bodyPlayer.y + Math.cos(_loc_1) * 1;
                _bodyPlayer.rotationY = _bodyPlayer.rotationY + 5;
                return;
            }// end function
            ;
                this._bodyPlayer = FrObjectPool.getObject(WeaponPlayer, this);
                if (this._bodyPlayer.meshUrl != this._config.mesh || this._bodyPlayer.animUrl != this._config.bone)
                {
                    (this._bodyPlayer as WeaponPlayer).load(this._config.mesh + ".md5mesh", this._config.texture, this._rect3D.renderList);
                }
                this._bodyPlayer.rotationY = 50;
                this._bodyPlayer.alpha = 1;
                (this._bodyPlayer as WeaponPlayer).play();
                this.killTween();
                this._bodyPlayer.rotationZ = -20;
                this._tween = TweenMax.to(this._bodyPlayer, 0.01, {ease:Quint.easeIn, repeat:-1, onRepeat:modelTurn});
                this._dy = -30;
            }
            this.adjustPlayerShow();
            var _loc_2:* = this._allScale;
            this._bodyPlayer.scaleZ = this._allScale;
            var _loc_2:* = _loc_2;
            this._bodyPlayer.scaleY = _loc_2;
            this._bodyPlayer.scaleX = _loc_2;
            this._rect3D.addObject3d(this._bodyPlayer, this._3dRectWidth / 2 + this._dx, this._3dRectHeight - 120 + this._dy, true, null, true);
            this.dispatchEvent(new WindowEvent(WindowEvent.SHOW));
            this.updatePosition();
            return;
        }// end function

        private function adjustPlayerShow() : void
        {
            if (this._config == null || this._bodyPlayer == null)
            {
                return;
            }
            switch(int(this._config.system))
            {
                case 100:
                {
                    this._bodyPlayer.rotationY = 80;
                    this._allScale = 1.2;
                    break;
                }
                case 101:
                {
                    this._allScale = 3.1;
                    this._dy = 120;
                    break;
                }
                case 102:
                {
                    this._dy = -80;
                    this._dx = -10;
                    this._allScale = 1.3;
                    break;
                }
                case 109:
                {
                    this._allScale = 1.8;
                    (this._bodyPlayer as ActionPlayer).changeAction(ActionName.Stand);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function killTween() : void
        {
            if (this._tween != null && this._tween.active)
            {
                this._tween.kill();
            }
            this._tween = null;
            return;
        }// end function

        public function get contentContainer() : Sprite
        {
            return this;
        }// end function

        protected function addToStageHandler(event:Event) : void
        {
            this.registerRect3D();
            this.dispatchEvent(new WindowEvent(WindowEvent.SHOW_END));
            this._isDisposed = false;
            return;
        }// end function

        protected function removeFromeStageHandler(event:Event) : void
        {
            this.killTween();
            this.dispatchEvent(new WindowEvent(WindowEvent.CLOSE_END));
            this._bodyPlayer = null;
            this._img2d = null;
            this._img2dTitle = null;
            this._img2dLabel = null;
            Rect3DManager.instance.disposeRect3d(this._rect3D);
            this._rect3D = null;
            this._isDisposed = true;
            return;
        }// end function

        public function updatePosition() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.POSITIONCHANGE));
            return;
        }// end function

        public function get contentTopOf3DSprite() : Sprite
        {
            return this._contentTopOf3DSprite;
        }// end function

        protected function set3dModel() : void
        {
            return;
        }// end function

        protected function modleLoadedHandler(param1 = null) : void
        {
            return;
        }// end function

    }
}
